/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.NewsTypeDAO;
import model.entities.NewsType;

/**
 *
 * @author THANMINH
 */
public class NewTypeServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
//        try {
//            /* TODO output your page here. You may use following sample code. */
//            out.println("<!DOCTYPE html>");
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet NewType</title>");            
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet NewType at " + request.getContextPath() + "</h1>");
//            out.println("</body>");
//            out.println("</html>");
//        } finally {
//            out.close();
//        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //String acction2 = request.getParameter("acction2");
        if(request.getParameter("action") != null){
            System.out.println(request.getParameter("action"));
        if (request.getParameter("action").equals("Add") ) {
            //response.sendRedirect("admin/viewnewtype.jsp");
            request.getRequestDispatcher("admin/viewnewtype.jsp").forward(request, response);

        } else {            
            int acction = Integer.parseInt(request.getParameter("action"));
            switch (acction) {

                case 1://edit
                    NewsType newstype = new NewsType();
                    NewsTypeDAO typedao = new NewsTypeDAO();
                    int id = Integer.parseInt(request.getParameter("id"));
                    newstype = typedao.getById(id);

                    request.setAttribute("newstype", newstype);
                    request.getRequestDispatcher("/admin/viewnewtype.jsp").forward(request, response);

                    break;
                case 2://hide
                    remove(request, response);
                    break;
                case 3://show
                    restore(request, response);
                    break;
                case 4://delete
                    delete(request, response);
                    break;
            }
        }
        }else{
            getAll(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String acction = request.getParameter("edit");
        if (acction == null) {
            getAll(request, response);
        } else {

            if (acction.equals("Save")) {
                NewsType newstype = new NewsType();
                NewsTypeDAO typedao = new NewsTypeDAO();
                System.out.println(request.getParameter("idNewsType"));
                if (request.getParameter("idNewsType") == "") {
                    //newstype.setIdNewType(Integer.parseInt(request.getParameter("idNewsType")));
                    newstype.setName(request.getParameter("name"));
                    newstype.setDescriptions(request.getParameter("descriptions"));
                    newstype.setIsActived(Boolean.valueOf(request.getParameter("isActived")));
                    typedao.addNewType(newstype);
                    response.sendRedirect("/LUD-Blog/NewTypeServlet");
                    //getAll(request, response);
                } else {
                    newstype.setIdNewType(Integer.parseInt(request.getParameter("idNewsType")));
                    newstype.setName(request.getParameter("name"));
                    newstype.setDescriptions(request.getParameter("descriptions"));
                    newstype.setIsActived(Boolean.valueOf(request.getParameter("isActived")));
                    typedao.editNewType(newstype);
                    response.sendRedirect("/LUD-Blog/NewTypeServlet");
                    //getAll(request, response);
                }
            } else if (acction.equals("Cancel")) {
                response.sendRedirect("/LUD-Blog/NewTypeServlet");
                //getAll(request, response);
            }
        }
    }

    // Function
    //getAll
    public void getAll(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        List<NewsType> newsType = new ArrayList();
        //NewsType entity = null;
        NewsTypeDAO typedao = new NewsTypeDAO();

        newsType = typedao.GetAll();

        request.setAttribute("newsType", newsType);
        request.getRequestDispatcher("/admin/newtype.jsp").forward(request, response);

    }

    //Add
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//            String name = request.getParameter("name");
//            String descriptions = request.getParameter("descriptions");

        NewsTypeDAO typedao = new NewsTypeDAO();
        NewsType news = new NewsType();

        news.setName(request.getParameter("name"));
        news.setDescriptions(request.getParameter("descriptions"));

        typedao.addNewType(news);
    }

    //Edit
    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    }

    //Delete
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        NewsTypeDAO typedao = new NewsTypeDAO();
        typedao.delete(Integer.parseInt(request.getParameter("id")));
        getAll(request, response);
    }

    //Remove
    public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        NewsTypeDAO typedao = new NewsTypeDAO();
        typedao.remove(Integer.parseInt(request.getParameter("id")));
        getAll(request, response);
    }

    //Restore
    public void restore(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        NewsTypeDAO typedao = new NewsTypeDAO();
        typedao.restore(Integer.parseInt(request.getParameter("id")));
        getAll(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
